# -*- coding: utf-8 -*-
# @日期    : 2020/12/29 13:46
# @作者  : 万方名
# @FileName: 持有股份计算器.py

import re
import requests
from bs4 import BeautifulSoup

bought_list = [['002405', 100], ['002714', 33.3], ['000725', 333], ['002594', 33.3], ['002475', 100]]
total_sum = 0

# 剩余子弹
bullet_balance = 1119
total_sum += bullet_balance * 0.333

for one_dict in bought_list:
    url_sz = 'https://xueqiu.com/S/SZ{}'.format(one_dict[0])
    url_sh = 'https://xueqiu.com/S/SH{}'.format(one_dict[0])

    # 获取方法F12
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.57"
    }

    response = requests.get(url_sz, headers=headers)
    if response.status_code == 404:
        response = requests.get(url_sh, headers=headers)

    parsed_data = BeautifulSoup(response.text, 'html5lib')
    scripts = parsed_data.select('script')
    gold_script = str(scripts[11])

    # 获取当前价格
    current_start = re.search('current"', gold_script, flags=0).span()[0]
    str_current = gold_script[current_start:current_start + 20]
    current = re.sub(r'[^0-9.]', '', str_current)
    total_sum += float(current) * one_dict[1]


print('总共持股{}元。'.format(total_sum))
